package com.ruoyi.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.sql.QueryHelp;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.product.domain.WoTagValue;
import com.ruoyi.product.domain.WorkOrder;
import com.ruoyi.product.domain.dto.WoTagValueDto;
import com.ruoyi.product.domain.vo.WoTagValueVo;
import com.ruoyi.product.mapper.WoTagValueMapper;
import com.ruoyi.product.service.WoTagValueService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 *
 */
@Service
public class WoTagValueServiceImpl implements WoTagValueService{
    @Resource
    WoTagValueMapper woTagValueMapper;


    @Override
    public Page<WoTagValueVo> getWoTagValueByPage(WoTagValueDto pageEntity) {
        MPJLambdaWrapper<WoTagValue> wrapper = new MPJLambdaWrapper<WoTagValue>()
                .selectAll(WoTagValue.class)//查询user表全部字段
                //记的测试阶段
                .eq(WoTagValue::getDelFlag, 0)
                .orderByDesc(WoTagValue::getCreateTime);
        wrapper = QueryHelp.getPredicateEx(wrapper, WoTagValue.class, pageEntity);
        Page<WoTagValueVo> userList = woTagValueMapper.selectJoinPage(new Page<>(pageEntity.getCurrentPage(), pageEntity.getPageSize()),   WoTagValueVo.class, wrapper);
        return userList;
    }

    @Override
    public void addWoTagValue(WoTagValueDto testDto) {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<WoTagValue> wrapper = new MPJLambdaWrapper<WoTagValue>()
                .eq(WoTagValue::getTagCode, testDto.getTagCode())
                .ne(WoTagValue::getDelFlag, 1);
        WoTagValue material = woTagValueMapper.selectOne(wrapper);
        if(null != material) {
            throw new GlobalException("该material已经存在");
        }
        WoTagValue test2 = new WoTagValue();
        BeanUtils.copyProperties(testDto, test2, new String[]{"id"} );
        test2.setDelFlag(false);
        test2.setCreateBy(userName);
        test2.setCreateTime(new Date());
        woTagValueMapper.insert(test2);
    }

    @Override
    public void updateWoTagValue(WoTagValueDto testDto) {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<WoTagValue> wrapper = new MPJLambdaWrapper<WoTagValue>()
                .eq(WoTagValue::getTagCode, testDto.getTagCode());
        WoTagValue woTagValue = woTagValueMapper.selectOne(wrapper);
        if(null == woTagValue) {
            throw new GlobalException("该WoTagValue不存在");
        }
        BeanUtils.copyProperties(testDto, woTagValue);
        woTagValue.setUpdateBy(userName);
        woTagValue.setUpdateTime(new Date());
        woTagValueMapper.updateById(woTagValue);
    }

    @Override
    public void delWoTagValue(WoTagValueDto testDto) {
        WoTagValue material = woTagValueMapper.selectById(testDto.getId());
        if(null == material) {
            throw new GlobalException("该WoTagValue不存在");
        }
        material.setDelFlag(true);
        woTagValueMapper.updateById(material);
    }
}




